package com.campol.activity;

import java.util.ArrayList;
import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;

import com.campol.MainActivity;
import com.campol.MainActivity.OnPostItemSelectedListener;
import com.campol.MainActivity.OnPostListChangedListener;
import com.campol.R;
import com.campol.model.PostModel;
import com.campol.utils.Constants;
import com.campol.view.MyItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapScreenActivity extends MapActivity implements OnPostListChangedListener, OnPostItemSelectedListener {

	private MapView mapView;
	
	private Drawable cameraPin, policePin;
	
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		init();
	}
	
	private void init() {
		setContentView(R.layout.map_screen);
		
		mapView = (MapView) findViewById(R.id.map_mapview);
		mapView.setEnabled(true);
		mapView.setClickable(true);
		mapView.setBuiltInZoomControls(true);
		mapView.getController().setZoom(10);
		
		((MainActivity)getParent()).addObserver(this);
		((MainActivity)getParent()).setOnPostItemSelectedListener(this);
		cameraPin = getResources().getDrawable(R.drawable.camera_pin);
		policePin = getResources().getDrawable(R.drawable.police_pin);
	}

	@Override
	protected void onStart() {
		super.onStart();
		PostModel item = ((MainActivity)getParent()).getTempPost();
		if (item != null) {
			ArrayList<PostModel> list = new ArrayList<PostModel>();
			list.add(item);
			onPostListChanged(list);
			((MainActivity)getParent()).setTempPost(null);
			return;
		}
		ArrayList<PostModel> items = ((MainActivity)getParent()).getPosts();
		if (items!=null) {
			onPostListChanged(items);
		} else {
			String type = PreferenceManager.getDefaultSharedPreferences(this).getString(SettingsActivity.SEARCH_AREA_KEY, Constants.GET_TYPE_NEARBY);
			((MainActivity)getParent()).makeRequestForPostsListWithType(type);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		String curTab = ((MainActivity)getParent()).getTabHost().getCurrentTabTag();
		if (MainActivity.TAB_MAP.equalsIgnoreCase(curTab)) {
			menu.clear();
			menu.add(0, Menu.FIRST, 0, "Refresh").setIcon(R.drawable.ic_menu_refresh);
			return true;
		}
		return false;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		String curTab = ((MainActivity)getParent()).getTabHost().getCurrentTabTag();
		if (MainActivity.TAB_MAP.equalsIgnoreCase(curTab)) {
			menu.clear();
			menu.add(0, Menu.FIRST, 0, "Refresh").setIcon(R.drawable.ic_menu_refresh);
			return true;
		}
		return false;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		String curTab = ((MainActivity)getParent()).getTabHost().getCurrentTabTag();
		if (MainActivity.TAB_MAP.equalsIgnoreCase(curTab)) {
			if (item.getItemId() == Menu.FIRST) {
				String type = PreferenceManager.getDefaultSharedPreferences(this).getString(SettingsActivity.SEARCH_AREA_KEY, Constants.GET_TYPE_NEARBY);
				((MainActivity)getParent()).makeRequestForPostsListWithType(type);
				return true;
			}
		}
		return false;
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	public void onPostListChanged(ArrayList<PostModel> items) {
		
		int minLat = Integer.MAX_VALUE;
		int maxLat = Integer.MIN_VALUE;
		int minLon = Integer.MAX_VALUE;
		int maxLon = Integer.MIN_VALUE;
		
		List<Overlay> mapOverlays = mapView.getOverlays();
		mapOverlays.clear();
		//CustomItemizedOverlay itemizedOverlayCamera = new CustomItemizedOverlay(cameraPin, this);
		MyItemizedOverlay itemizedOverlayPolice = new MyItemizedOverlay(policePin, this);
		MyItemizedOverlay itemizedOverlayCamera = new MyItemizedOverlay(cameraPin, this);
		if (items == null) return;
		for (PostModel pm : items) {
			int lat = (int)(pm.Latitude*1E6);
			int lon = (int)(pm.Longitude*1E6);

			maxLat = Math.max(lat, maxLat);
			minLat = Math.min(lat, minLat);
			maxLon = Math.max(lon, maxLon);
			minLon = Math.min(lon, minLon);
		      
			GeoPoint point = new GeoPoint(lat, lon);
			OverlayItem overlayitem = new OverlayItem(point, pm.Road, pm.Place);
			if (pm.IsCamera) {
				itemizedOverlayCamera.addOverlay(overlayitem);
			} else {
				itemizedOverlayPolice.addOverlay(overlayitem);
			}
		}
		if (itemizedOverlayCamera.getOverlaysCount() > 0) {
			mapOverlays.add(itemizedOverlayCamera);
		}
		if (itemizedOverlayPolice.getOverlaysCount() > 0) {
			mapOverlays.add(itemizedOverlayPolice);
		}
		mapView.postInvalidate();
		mapView.getController().zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));
		mapView.getController().animateTo(new GeoPoint( (maxLat + minLat)/2, (maxLon + minLon)/2 )); 
	}

	@Override
	public void onPostItemSelected(PostModel item) {
		if (item != null) {
			ArrayList<PostModel> list = new ArrayList<PostModel>();
			list.add(item);
			onPostListChanged(list);
			((MainActivity)getParent()).setTempPost(null);
		}
	}
}
